package com.zou.application.controller.customer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zou.application.dao.ClassifyEntityMapper;
import com.zou.application.dao.ProductEntityMapper;
import com.zou.application.entity.ClassifyEntity;
import com.zou.application.entity.ProductEntity;

@Controller
@RequestMapping("/customer")
public class ProductsController {
	
	@Autowired
	private ProductEntityMapper productDao;
	@Autowired
	private ClassifyEntityMapper classifyDao;
	
	@RequestMapping("/products")
	public ModelAndView indexPage(HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		String classId = request.getParameter("classId");
		String minPrice = request.getParameter("minPrice");
		String maxPrice = request.getParameter("maxPrice");
		String search = request.getParameter("search");
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("classId", classId);
			param.put("minPrice", minPrice);
			param.put("maxPrice", maxPrice);
			param.put("search", search);
			List<ProductEntity> productEntities = productDao.listByMap(param);
			List<ClassifyEntity> classifyEntities = classifyDao.listByMap(param);
			mv.addObject("minPrice",minPrice);
			mv.addObject("maxPrice",maxPrice);
			mv.addObject("search",search);;
			mv.addObject("products",productEntities);
			mv.addObject("classifys",classifyEntities);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.setViewName("/customer/products");
		return mv;
	}
	
	
	/**
	 * 查看商品详情
	 * @param request
	 * @return
	 */
	@RequestMapping("/signle")
	public ModelAndView productList(HttpServletRequest request){
		ModelAndView mv = new ModelAndView();
		String id = request.getParameter("id");
		try {
			ProductEntity product = productDao.selectById(Integer.parseInt(id));
			Map<String, Object> param = new HashMap<String, Object>();
			List<ClassifyEntity> classifyEntities = classifyDao.listByMap(param);
			mv.addObject("classifys",classifyEntities);
			mv.addObject("product",product);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.setViewName("/customer/single");
		return mv;
	}
}
